﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://wpfimageviewer.codeplex.com
using System;
using System.Collections.Specialized;
using System.ComponentModel;
using System.IO;
using AForge.Imaging;
using BrainTechLLC;
using BrainTechLLC.FilterExtensions;
using ImageAppDataModels;
using ImageAppViewModels;
using System.Collections.Generic;

namespace WpfImageDuplicates
{
    public class DataLoader
    {
        protected Window1 _parentWindow;
        public static AllMetaData _metaData;
        public List<Group> _groups;
        public List<FileImageGroup> _fileImageGroups;
        
        public DataLoader(Window1 parentWindow)
        {
            _parentWindow = parentWindow;
        }
        
        public void LoadMetaData()
        {
            if (App.DataProvider.Exists(FilePaths._groupsFilePath))
            {
                byte[] bytes = App.DataProvider.ReadAllImageBytes(FilePaths._groupsFilePath);
                _groups = bytes.DeserializeFromCompressedBinary(false) as List<Group>;
            }

            if (App.DataProvider.Exists(FilePaths._fileImageGroupsFilePath))
            {
                byte[] bytes = App.DataProvider.ReadAllImageBytes(FilePaths._fileImageGroupsFilePath);
                _fileImageGroups = bytes.DeserializeFromCompressedBinary(false) as List<FileImageGroup>;
            }

            if (_groups == null)
            {
                _groups = new List<Group>();
            }

            if (_fileImageGroups == null)
            {
                _fileImageGroups = new List<FileImageGroup>();
            }

            if (App.DataProvider.Exists(FilePaths._metaDataFilePath))
            {
                _metaData = AllMetaData.ReadFromDisk(FilePaths._metaDataFilePath, App.DataProvider);
            }
            if (_metaData == null)
            {
                _metaData = new AllMetaData();
            }
        }

        public void SaveGroups(GroupCollectionViewModel allGroups)
        {
            List<Group> groups = new List<Group>();
            allGroups.ForEach(o => groups.Add(new Group() { GroupName = o.Item.GroupName }));
            byte[] bytes = groups.SerializeToCompressedBinary(false);
            App.DataProvider.WriteAllBytes(FilePaths._groupsFilePath, bytes);
        }

        public void SaveFileImageGroups(List<FileImageGroup> list)
        {
            byte[] bytes = list.SerializeToCompressedBinary(false);
            App.DataProvider.WriteAllBytes(FilePaths._fileImageGroupsFilePath, bytes);
        }
    }
}
